﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Geometry
{
	static class Intersection
	{

		static public List<GeomVector2> getIntersection(Rect rect, Circle circle)
		{
			List<GeomVector2> intersections = new List<GeomVector2>();

			float circleX = circle.Center.X;
			float circleY = circle.Center.Y;

			// Cuadratic equation x^2*a + x*b + c = 0
			float a = (float)(1 + Math.Pow((double)rect.Slope, 2));
			float b = -2 * circleX + 2 * rect.Slope * (rect.IndependentTerm - circleY);
			float c = (float)(Math.Pow((double)circleX, 2) + Math.Pow((double)(rect.IndependentTerm - circleY), 2) - Math.Pow((double)circle.Radius, 2));
			
			if (Math.Pow(b, 2) - 4 * a * c < 0) return intersections; // No intersection

			float sqrtValue = (float)(Math.Pow(Math.Pow(b, 2) - 4 * a * c, 1 / 2F));
			float Xa = (-b + sqrtValue) / (2 * a);
			float Xb = (-b - sqrtValue) / (2 * a);

			intersections.Add(new GeomVector2(Xa, Xa * rect.Slope + rect.IndependentTerm));
			if (sqrtValue != 0) intersections.Add(new GeomVector2(Xb, Xb * rect.Slope + rect.IndependentTerm));

			return intersections;
		}

		static public List<GeomVector2> getIntersection(Rect rectA, Rect rectB)
		{
			List<GeomVector2> intersections = new List<GeomVector2>();

			if (rectA.Slope == rectB.Slope)
			{
				if (rectA.IndependentTerm != rectB.IndependentTerm) return intersections;
				else throw new AllPointsInIntersectionException();
			}

			return intersections;
		}

	}
}
